package com.ruyuan.eshop.buyer.controller.trade;

import com.ruyuan.eshop.buyer.domain.query.OrderListQuery;
import com.ruyuan.eshop.buyer.domain.request.OrderCancelRequest;
import com.ruyuan.eshop.common.domain.vo.*;
import com.ruyuan.eshop.framework.context.user.UserContext;
import com.ruyuan.eshop.framework.context.user.model.Buyer;
import com.ruyuan.eshop.framework.core.WebPage;
import com.ruyuan.eshop.framework.utils.StringUtil;
import com.ruyuan.eshop.common.enums.OrderPermission;
import com.ruyuan.eshop.common.domain.dto.OrderDetailQueryParam;
import com.ruyuan.eshop.common.domain.dto.OrderQueryParam;
import com.ruyuan.eshop.common.enums.OrderTagEnum;
import com.ruyuan.eshop.common.enums.PaymentTypeEnum;
import com.ruyuan.eshop.common.service.trade.order.OrderLogService;
import com.ruyuan.eshop.common.service.trade.order.OrderOperateService;
import com.ruyuan.eshop.common.service.trade.order.OrderQueryService;
import com.ruyuan.eshop.common.service.trade.order.TradeQueryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员订单相关控制器
 * @author pwd
 * @version 1.0
 */

@Api(description = "会员订单API")
@RestController
@RequestMapping("/api/v1.0/trade/orders")
@Validated
public class OrderBuyerController {

    @Autowired
    private OrderQueryService orderQueryService;

    @Autowired
    private OrderOperateService orderOperateService;

    /**
     * 查询会员订单列表
     * @param orderListQuery
     * @return
     */
    @ApiOperation(value = "查询会员订单列表")
    @GetMapping
    public WebPage<OrderLineVO> list(OrderListQuery orderListQuery) {
        String orderStatus = orderListQuery.getOrderStatus();
        if (StringUtil.isEmpty(orderStatus)) {
            orderStatus = "ALL";
        }
        OrderTagEnum.valueOf(orderStatus);
        Buyer buyer = UserContext.getBuyer();
        OrderQueryParam param = new OrderQueryParam();
        param.setGoodsName(orderListQuery.getGoodsName());
        param.setTag(orderStatus);
        param.setMemberId(buyer.getUid());
        param.setKeywords(orderListQuery.getKeyWords());
        param.setPageNo(orderListQuery.getPageNo());
        param.setPageSize(orderListQuery.getPageSize());
        param.setStartTime(orderListQuery.getStartTime());
        param.setEndTime(orderListQuery.getEndTime());
        WebPage page = this.orderQueryService.list(param);
        //货到付款的订单不允许线上支付
        List<OrderLineVO> list = page.getData();
        for (OrderLineVO order : list) {
            if (PaymentTypeEnum.COD.value().equals(order.getPaymentType())) {
                order.getOrderOperateAllowableVO().setAllowPay(false);
            }
        }
        page.setData(list);
        return page;
    }

    /**
     * 查询单个订单明细
     * @param orderSn
     * @return
     */
    @ApiOperation(value = "查询单个订单明细")
    @GetMapping(value = "/{order_sn}")
    public OrderDetailVO get(@PathVariable("order_sn") String orderSn) {
        Buyer buyer = UserContext.getBuyer();
        OrderDetailQueryParam queryParam = new OrderDetailQueryParam();
        queryParam.setBuyerId(buyer.getUid());
        OrderDetailVO detailVO = this.orderQueryService.getModel(orderSn, queryParam);

        if (detailVO.getNeedReceipt().intValue() == 1) {
            detailVO.setReceiptHistory(null);
        }

        //货到付款的订单不允许线上支付
        if (PaymentTypeEnum.COD.value().equals(detailVO.getPaymentType())) {
            detailVO.getOrderOperateAllowableVO().setAllowPay(false);
        }

        return detailVO;
    }


    /**
     * 确认收货
     * @param orderSn
     * @return
     */
    @ApiOperation(value = "确认收货")
    @PostMapping(value = "/{order_sn}/rog")
    public String rog(@PathVariable("order_sn") String orderSn) {

        Buyer buyer = UserContext.getBuyer();
        RogVO rogVO = new RogVO();
        rogVO.setOrderSn(orderSn);
        rogVO.setOperator(buyer.getUsername());

        orderOperateService.rog(rogVO, OrderPermission.buyer);
        return "";
    }

    /**
     * 取消订单
     * @param orderSn
     * @param orderCancelRequest
     * @return
     */
    @ApiOperation(value = "取消订单")
    @PostMapping(value = "/{order_sn}/cancel")
    public String cancel(@PathVariable("order_sn") String orderSn, @RequestBody OrderCancelRequest orderCancelRequest) {

        Buyer buyer = UserContext.getBuyer();
        CancelVO cancelVO = new CancelVO();
        cancelVO.setOperator(buyer.getUsername());
        cancelVO.setOrderSn(orderSn);
        cancelVO.setReason(orderCancelRequest.getReason());
        orderOperateService.cancel(cancelVO, OrderPermission.buyer);
        return "";
    }

    /**
     * 查询订单状态的数量
     * @return
     */
    @ApiOperation(value = "查询订单状态的数量")
    @GetMapping(value = "/status-num")
    public OrderStatusNumVO getStatusNum() {
        Buyer buyer = UserContext.getBuyer();
        OrderStatusNumVO orderStatusNumVO = this.orderQueryService.getOrderStatusNum(buyer.getUid(), null);
        return orderStatusNumVO;
    }

    /**
     * 根据交易编号查询订单列表
     * @param tradeSn
     * @return
     */
    @ApiOperation(value = "根据交易编号查询订单列表")
    @GetMapping(value = "/{trade_sn}/list")
    public List<OrderDetailVO> getOrderList(@PathVariable("trade_sn") String tradeSn) {
        Buyer buyer = UserContext.getBuyer();
        List<OrderDetailVO> orderDetailVOList = this.orderQueryService.getOrderByTradeSn(tradeSn, buyer.getUid());
        return orderDetailVOList;
    }

}
